﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using DevExpress.Web.ASPxMenu;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxClasses;

namespace SGDemo.Permission
{
    public partial class UserItem : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);
       
        }

       /// <summary>
       /// 部门的回调函数
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        protected void dept_OnCallback(object sender, CallbackEventArgsBase e)
        {
            string userkeyid = e.Parameter;
            lbdept.DataSource = _permissionService.Dept_GetByUserKeyid(userkeyid);
            lbdept.DataBind();
        }

        /// <summary>
        /// 岗位的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void role_OnCallback(object sender, CallbackEventArgsBase e)
        {
            string userkeyid = e.Parameter;
            lbrole.DataSource = _permissionService.Role_GetByUserKeyid(userkeyid);
            lbrole.DataBind();
        }

        /// <summary>
        /// 菜单项绑定事件 - 实现操作权限控制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void menuHeader_ItemDataBound(object sender, MenuItemEventArgs e)
        {
            IHierarchyData itemHierarchyData = (IHierarchyData)e.Item.DataItem;
            XmlElement element = (XmlElement)itemHierarchyData.Item;
            //if (element.Attributes["Index"] == null)
            //    return;
            //int imgIndex = int.Parse(element.Attributes["Index"].Value);
            //e.Item.Image.SpriteProperties.CssClass = string.Format("componentImage_{0}", imgIndex);
            //e.Item.Visible = false;
        }

        [AjaxPro.AjaxMethod]
        public bool IsExists(string userName)
        {
            return _permissionService.User_IsExist(userName);
        }


        /// <summary>
        /// 新增保存
        /// </summary>
        /// <remarks>
        ///     0，失败
        ///     1，成功
        ///     2，用户名存在
        /// </remarks>
        [AjaxPro.AjaxMethod]
        public int SaveForAdd(UserDetails user)
        {
            if (_permissionService.User_IsExist(user.Username))
            {
                return 2;
            }

            user.CreateName = ApplicationContext.Current.UserName;
            user.CreateDate = System.DateTime.Now;
            user.Password = SGRapidForm.Common.DESEncrypt.Encrypt(user.Password);
            if (_permissionService.User_Add(user))
            {
                return 1;
            }
            else {
                return 0;
            }
        }

        /// <summary>
        /// 获取一位用户明细
        /// </summary>
        /// <param name="keyid">用户Keyid</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public UserDetails GetUserByKeyid(string keyid)
        {
            UserDetails user = _permissionService.User_GetByKeyid(keyid);
            user.Password = DESEncrypt.Decrypt(user.Password);
            return user;
        }

        /// <summary>
        /// 编辑保存
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveForEdit(UserDetails user)
        {
            //加入Session等服务端信息
            user.ModifyName = ApplicationContext.Current.UserName;
            user.ModifyDate = System.DateTime.Now;
            user.Password = SGRapidForm.Common.DESEncrypt.Encrypt(user.Password);
            bool ret = _permissionService.User_Update(user);
            return ret;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyid">用户Keyid</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool Delete(string keyid)
        {
            return _permissionService.User_Delete(keyid);
        }
    }
}